package com.lwj.liwanjia.adapter.indent;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.bean.MyIndentBean;
import com.lwj.liwanjia.config.OnClickPosition;
import com.lwj.liwanjia.config.OnItemChildClickListener;
import com.lwj.liwanjia.config.OnItemClickListener;
import com.lwj.liwanjia.utils.TimeCountDown1;
import com.lwj.liwanjia.view.activity.indent.UnpaidActivity;
import com.superrecycleview.superlibrary.adapter.BaseViewHolder;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/9/22.
 */

public class UnTakeAdapter extends RecyclerView.Adapter<UnTakeAdapter.MyVieHolder> {

    private Context mContext;
    private List<MyIndentBean.DataBean> list;
    private boolean flag;//用来判断是否是详情 true代表是
    private LayoutInflater inflater;
    private OnItemClickListener listener;
    private OnItemChildClickListener childListener;
    private OnClickPosition clickPosition;

    public void setClickPosition(OnClickPosition clickPosition) {
        this.clickPosition = clickPosition;
    }

    public void setChildListener(OnItemChildClickListener childListener) {
        this.childListener = childListener;
    }

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public UnTakeAdapter(Context mContext, List<MyIndentBean.DataBean> list, boolean flag) {
        this.mContext = mContext;
        this.list = list;
        this.flag = flag;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public MyVieHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (list.get(viewType).getTitle().equals("购物")) {
            View view = inflater.inflate(R.layout.unpaid_item, parent, false);
            UnTakeAdapter.MyVieHolder holder = new MyVieHolder(view);
            return holder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(MyVieHolder holder, final int position) {
        final MyIndentBean.DataBean.GoodsBean goods = list.get(position).getGoods();
        holder.llFukuan.setVisibility(View.GONE);
        holder.rlIndentBtn.setVisibility(View.VISIBLE);
        holder.tvDeleteIndent.setVisibility(View.VISIBLE);
        if (goods.getStatus().equals("待发货")) {
            holder.tvState.setText("待发货");
            holder.tvDeleteIndent.setText("取消订单");
            holder.tvQuxiaoIndent.setText("联系商家");

        } else {
            holder.tvState.setText("待收货");
            holder.tvDeleteIndent.setText("联系商家");
            holder.tvQuxiaoIndent.setText("确认收货");
        }
        holder.tvMarket.setText(goods.getShopname());
        holder.tvShangchengNumber.setText("共" + goods.getAmount() + "件商品 合计：");
        holder.tvShangchengPrice.setText("￥" + goods.getTotalpay() + "(含运费￥0.00)");
        Glide.with(mContext).load(goods.getShopimage()).error(R.mipmap.shop_image_2).into(holder.ivMarket);
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        holder.rlvGouwuche.setLayoutManager(manager);
        holder.rlvGouwuche.setRefreshEnabled(false);
        holder.rlvGouwuche.setLoadMoreEnabled(false);
        ShopUnpaidAdater1 adapter = new ShopUnpaidAdater1(mContext, goods.getCommodity(), flag);
        holder.rlvGouwuche.setAdapter(adapter);
        adapter.setOnItemClickListener(new SuperBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object item, int position1) {
                if (clickPosition != null) {
                    clickPosition.clickPosition(position);
                }
                Intent intent = new Intent(mContext, UnpaidActivity.class);
                intent.putExtra("tradeId", list.get(position).getGoods().getTradeId());
                mContext.startActivity(intent);
            }
        });
        if (listener != null) {
            holder.rlGouwu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.OnItemClick(v, position);
                }
            });
        }
        if (childListener != null) {
            holder.tvDeleteIndent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    childListener.OnItemChildClick(v, goods.getStatus(), position);
                }
            });
            holder.tvQuxiaoIndent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    childListener.OnItemChildClick(v, goods.getStatus(), position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyVieHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.rl_gouwu)
        RelativeLayout rlGouwu;
        @BindView(R.id.iv_market)
        ImageView ivMarket;
        @BindView(R.id.tv_market)
        TextView tvMarket;
        @BindView(R.id.tv_state)
        TextView tvState;
        @BindView(R.id.rl_market)
        RelativeLayout rlMarket;
        @BindView(R.id.rlv_gouwuche)
        SuperRecyclerView rlvGouwuche;
        @BindView(R.id.line)
        View line;
        @BindView(R.id.tv_shangcheng_price)
        TextView tvShangchengPrice;
        @BindView(R.id.tv_shangcheng_number)
        TextView tvShangchengNumber;
        @BindView(R.id.rl_jiage)
        RelativeLayout rlJiage;
        @BindView(R.id.line1)
        View line1;
        @BindView(R.id.tv_minute)
        TextView tvMinute;
        @BindView(R.id.tv_second)
        TextView tvSecond;
        @BindView(R.id.ll_fukuan)
        LinearLayout llFukuan;
        @BindView(R.id.tv_quxiao_indent)
        TextView tvQuxiaoIndent;
        @BindView(R.id.tv_delete_indent)
        TextView tvDeleteIndent;
        @BindView(R.id.rl_indent_btn)
        RelativeLayout rlIndentBtn;
        @BindView(R.id.indent_btn)
        RelativeLayout indentBtn;

        public MyVieHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
